package main

import (
	"fmt"
	"ginDemo/2_demo/model"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

func main() {
	// 连接数据库
	// 用户名:密码@tcp(ip:port)/数据库名?charset=utf8&parseTime=True&loc=Local
	dsn := "root:123456@tcp(localhost:3306)/lianjia?charset=utf8&parseTime=True&loc=Local"
	db, err := gorm.Open("mysql", dsn)
	if err != nil {
		panic(err)
	}

	defer db.Close() // 关闭空闲的连接
	// db.AutoMigrate(&model.User{})
	// u := model.User{Name: "zshang22", Age: 18, Addr: "xhkajh", Pic: "/vick", Phone: "18887212314"}
	// u1 := model.User{Name: "lishi2", Age: 18, Addr: "xhkajh", Pic: "/vick", Phone: "18887212314"}

	// 增
	// db.Create(&u)
	// db.Create(&u1)

	// 查询
	var user model.User
	// db.First(&user, 1) // 第二参数是where，默认是id
	// fmt.Println(user)

	db.First(&user, "name=?", "zshang22")
	fmt.Println(user)

	// 更新
	db.Model(&user).Update("age", 20)

	// 删除
	// var user1 model.User

	db.Delete(&user)
}
